package {{pkgName}}.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.common.biz.controller.BaseFlowController;
import com.fuyao.cloud.common.biz.dto.PageQuery;
import com.fuyao.cloud.common.core.constant.SecurityConstants;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.log.annotation.SysLog;
import {{pkgName}}.entity.{{entityName}};
import {{pkgName}}.service.{{entityName}}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/{{requestPath}}")
@Api(value = "{{requestPath}}", tags = "{{moduleName}}")
public class {{entityName}}Controller extends BaseFlowController<{{entityName}}, {{entityName}}Service> {

    /**
     * 分页查询
     *
     * @param pageQuery 分页对象
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @PostMapping("/page")
    public R<Page<{{entityName}}>> getPage(PageQuery<{{entityName}}> pageQuery) {
        return R.ok(service.getPage(pageQuery));
    }


    /**
     * 通过id查询
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R<{{entityName}}> getById(@PathVariable("id") Long id) {
        {{entityName}} entity = service.getById(id);
        return R.ok(entity);
    }

    /**
     * 新增
     *
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
    public R<{{entityName}}> save(@RequestBody {{entityName}} entity) {
        service.save(entity);
        return getById(entity.getId());
    }

    /**
     * 修改
     *
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
    public R<{{entityName}}> updateById(@RequestBody {{entityName}} entity) {
        service.updateById(entity);
        return getById(entity.getId());
    }

    /**
     * 创建方法
     */
    @GetMapping(value = "/create")
    public R<{{entityName}}> create() {
        return R.ok(service.create());
    }

    /**
     * 批量删除方法
     *
     * @return R
     */
    @ApiOperation(value = "批量删除", notes = "批量删除")
    @SysLog("批量删除")
    @DeleteMapping("/batchDelete")
    public R<Boolean> batchDelete(@RequestBody List<Long> ids) {
        service.removeByIds(ids);
        ids.forEach(id -> remoteFlowableService.deleteByBusinessKey(getBusinessKey(id), SecurityConstants.FROM_IN));
        return R.ok(Boolean.TRUE);
    }

    @Override
    protected String getBusinessKey(Object id) {
        return "{{docType}}:" + id;
    }
}
